/**
 * List根据key取value
 *
 * @param key key值
 * @param list
 * @returns {string}
 */
export function getValue(key, list) {
  if (typeof list !== "object" || !list.length) {
    return "";
  }
  let listValue = "";
  for (let i = 0; i < list.length; i += 1) {
    if (list[i] && list[i].key === key) {
      listValue = list[i].value;
      break;
    }
  }
  return listValue;
}
/**
 * List根据typeName获取typeCode值
 * @param {*} name 值
 * @param {*} list 数据List
 */
export function getTypeCode(name, list) {
  let value = "";
  if (typeof list !== "object" || !list.length) {
    return value;
  }
  for (let i = 0; i < list.length; i += 1) {
    if (list[i] && list[i].typeName === name) {
      value = list[i].typeCode;
      break;
    }
  }
  return value;
}
/**
 * List根据typeCode取typeName
 *
 * @param key
 * @param list
 * @returns {string}
 */
export function getTypeName(key, list) {
  let value = "";
  if (typeof list !== "object" || !list.length) {
    return value;
  }
  for (let i = 0; i < list.length; i += 1) {
    if (list[i] && list[i].typeCode === key) {
      value = list[i].typeName;
      break;
    }
  }
  return value;
}

/**
 * (XXX XXX XXX)
 */
/**
 * 多层List根据typeCode取typeName
 *
 * @param typeCodeList typeCode [xxx,xxx-xxx,xxx-xxx-xxx]
 * @param list [a1,a2,b1,b2]
 * @returns {string}
 */
export function getTypeNameByList(typeCodeList, list) {
  let value = "";
  if (typeof typeCodeList !== "object" || !typeCodeList.length) {
    return value;
  }
  for (let i = 0; i < typeCodeList.length; i += 1) {
    for (let j = 0; j < list.length; j += 1) {
      if (typeCodeList[i] === list[j].typeCode) {
        value = `${value} ${list[j].typeName}`;
        break;
      }
    }
  }
  return value;
}

/**
 * (XXX XXX XXX)
 */
/**
 * 多层List根据typeCode取typeName
 *
 */
export function getTypeNameByAreaList(typeCodeList, list) {
  let value = [];
  if (typeof typeCodeList !== "object" || !typeCodeList.length) {
    return value;
  }
  for (let i = 0; i < typeCodeList.length; i += 1) {
    for (let j = 0; j < list.length; j += 1) {
      if (typeCodeList[i] === list[j].code) {
        value.push(`${list[j].name}`);
      }
      for (let k = 0; k < list[j].children.length; k++) {
        if (typeCodeList[i] === list[j].children[k].code) {
          value.push(`${list[j].children[k].name}`);
        }
        for (let s = 0; s < list[j].children[k].children.length; s++) {
          if (typeCodeList[i] === list[j].children[k].children[s].code) {
            value.push(`${list[j].children[k].children[s].name}`);
          }
        }
      }
    }
  }
  return value;
}
/**
 * 多个typeCode在多层list中取valuetypeName
 *
 * @param typeCodeMultipleList [[xxx,xxx-xxx,xxx-xxx-xxx],[xxx,xxx-xxx,xxx-xxx-xxx]]
 * @param list [a1,a2,b1,b2]
 * @returns {string}  (XXX XXX XXX,YYYY YYY YYY)
 */
export function getTypeNameByMultipleList(typeCodeMultipleList, list) {
  let value = "";
  if (typeof typeCodeMultipleList !== "object" || !typeCodeMultipleList.length) {
    return value;
  }
  for (let i = 0; i < typeCodeMultipleList.length; i += 1) {
    value = `${value},${getTypeNameByList(typeCodeMultipleList[i].split("-"), list)}`;
  }
  value = value.substr(1, value.length - 1);
  return value;
}
/**
 * 根据父节点code取list
 * @param parentTypeCode
 * @param list
 */
function getListByParentTypeCode(parentTypeCode, list) {
  if (typeof list !== "object" || !list.length) {
    return [];
  }
  const listByParentTypeCode = [];
  for (let i = 0; i < list.length; i += 1) {
    if (parentTypeCode === list[i].parentTypeCode) {
      listByParentTypeCode.push({
        value: list[i].typeCode,
        label: list[i].typeName,
      });
    }
  }
  return listByParentTypeCode;
}

/**
 * 将两层list转化成"X-X-X,X-X-X"形式的字符串
 *
 * @param list [[x,x,x],[y,y,y]]
 * @returns {string|undefined} x-x-x,y-y-y
 */
export function formatMultipleListToString(list) {
  let value = "";
  if (typeof list !== "object" || !list.length) {
    return undefined;
  }
  const listStr = [];
  for (let i = 0; i < list.length; i += 1) {
    listStr.push(list[i].join("-"));
  }
  value = listStr.join(",");

  return value;
}

/**
 * 分层list整理
 *
 * @param list [a1,a2,b1,b2]
 * @returns {*} [{a1, children: a2}, {b1, children: b2}]
 */
export function formatList(list) {
  if (typeof list !== "object" || !list.length) {
    return [];
  }
  const formatListData = getListByParentTypeCode(null, list);
  for (let i = 0; i < formatListData.length; i += 1) {
    formatListData[i].children = getListByParentTypeCode(formatListData[i].value, list);
    for (let j = 0; j < formatListData[i].children.length; j += 1) {
      if (getListByParentTypeCode(formatListData[i].children[j].value, list).length !== 0) {
        formatListData[i].children[j].children = getListByParentTypeCode(formatListData[i].children[j].value, list);
      }
    }
  }
  return formatListData;
}

/**
 * 一个节点下是否存在子节点
 *
 * @param parentTypeCode
 * @param list
 * @returns {boolean}
 */
function hasChild(parentTypeCode, list) {
  if (typeof list !== "object" || !list.length) {
    return false;
  }
  for (let i = 0; i < list.length; i += 1) {
    if (list[i] && list[i].parentTypeCode === parentTypeCode) {
      return true;
    }
  }
  return false;
}
/**
 * 根据选项名获Code取值
 * @param {*} name 项目名
 * @param {*} list 数据List
 * @param {*} count 获取数据层数
 */
export function getCodeByName(name, list) {
  let value = "";
  if (typeof list !== "object" || !list.length) {
    return value;
  }
  let parentTypeCodeData = "";
  for (let i = 0; i < list.length; i += 1) {
    if (list[i] && list[i].typeName === name) {
      if (!hasChild(list[i].typeCode, list)) {
        parentTypeCodeData = list[i].parentTypeCode === null ? "" : list[i].parentTypeCode;
        value = `${list[i].parentTypeCode === null ? "" : list[i].parentTypeCode}${list[i].parentTypeCode === null ? "" : "-"}${list[i].typeCode}`;
        break;
      }
    }
  }
  let i = 0;
  while (parentTypeCodeData) {
    for (i = 0; i < list.length; i += 1) {
      if (parentTypeCodeData === list[i].typeCode) {
        parentTypeCodeData = list[i].parentTypeCode === null ? "" : list[i].parentTypeCode;
        if (parentTypeCodeData) {
          value = `${parentTypeCodeData}${parentTypeCodeData === "" ? "" : "-"}${value}`;
        }
      }
    }
  }
  return value;
}
/**
 * 根据选项名获Code取值
 * @param {*} name 项目名
 * @param {*} list 数据List
 * @param {*} count 获取数据层数
 */
export function getNameByCode(name, list) {
  let value = "";
  if (typeof list !== "object" || !list.length) {
    return value;
  }
  let parentTypeCodeData = "";
  for (let i = 0; i < list.length; i += 1) {
    if (list[i] && list[i].typeName === name) {
      if (!hasChild(list[i].typeCode, list)) {
        parentTypeCodeData = list[i].parentTypeCode === null ? "" : list[i].parentTypeCode;
        value = `${list[i].parentTypeCode === null ? "" : list[i].parentTypeCode}${list[i].parentTypeCode === null ? "" : ","}${list[i].typeCode}`;
        break;
      }
    }
  }
  let i = 0;
  while (parentTypeCodeData) {
    for (i = 0; i < list.length; i += 1) {
      if (parentTypeCodeData === list[i].typeCode) {
        parentTypeCodeData = list[i].parentTypeCode === null ? "" : list[i].parentTypeCode;
        if (parentTypeCodeData) {
          value = `${parentTypeCodeData}${parentTypeCodeData === "" ? "" : ","}${value}`;
        }
      }
    }
  }
  return value;
}

export default {
  getNameByCode,
  getValue,
  formatMultipleListToString,
  getTypeName,
  getTypeNameByList,
  getTypeNameByMultipleList,
  formatList,
};
